﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormPipeCheckedCResult : System.Windows.Forms.Form
	{
		public FormPipeCheckedCResult(ExternalCommandData data)
		{
			this.m_transaction = new Transaction(data.Application.ActiveUIDocument.Document, "重合检查");
			this.m_uiApp = data.Application;
			this.m_rvtDoc = data.Application.ActiveUIDocument.Document;
			this.m_uiDoc = data.Application.ActiveUIDocument;
			this.m_rvtApp = data.Application.Application;
			this.m_rvtSel = data.Application.ActiveUIDocument.Selection;
			this.m_detector = new Detector(this.m_rvtDoc);
			IList<Element> list = new List<Element>();
			foreach (ElementId elementId in this.m_rvtSel.GetElementIds())
			{
				list.Add(this.m_rvtDoc.GetElementById(elementId));
			}
			this.GetSelectionElems(list);
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			try
			{
				this.m_transaction.Start();
				this.GetOffValue();
				this.InitTreeView();
				if (this.m_tv.Nodes.Count > 0)
				{
					base.ShowDialog();
				}
				else
				{
					YJKMessageBox.Information("没有找到重合管线。");
					base.ShowDialog();
				}
				this.m_transaction.Commit();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
				this.m_transaction.RollBack();
			}
		}

		public FormPipeCheckedCResult(ExternalCommandData data, IList<Element> elements)
		{
			this.m_transaction = new Transaction(data.Application.ActiveUIDocument.Document, "重合检查");
			this.m_uiApp = data.Application;
			this.m_rvtDoc = data.Application.ActiveUIDocument.Document;
			this.m_uiDoc = data.Application.ActiveUIDocument;
			this.m_rvtApp = data.Application.Application;
			this.m_rvtSel = data.Application.ActiveUIDocument.Selection;
			this.m_detector = new Detector(this.m_rvtDoc);
			this.GetSelElems(elements);
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			try
			{
				this.m_transaction.Start();
				this.GetOffValue();
				this.InitTreeView();
				if (this.m_tv.Nodes.Count > 0)
				{
					base.ShowDialog();
				}
				else if (elements.Count > 1)
				{
					YJKMessageBox.Information("没有找到重合管线。");
					base.ShowDialog();
				}
				this.m_transaction.Commit();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
				this.m_transaction.RollBack();
			}
		}

		private void InitTreeView()
		{
			this.m_tv = this.ShowTreeview();
			this.ResultTv.Controls.Add(this.m_tv);
			this.m_tv.Dock = DockStyle.Fill;
			this.m_tv.AfterSelect += this.ResultTv_AfterSelect;
			this.m_tv.BeforeSelect += this.ResultTv_BeforeSelect;
			if (this.m_tv.Nodes.Count > 0)
			{
				this.m_tv.SelectedNode = this.m_tv.Nodes[0];
			}
		}

		private void GetSelElems(IList<Element> elems)
		{
			if (this.m_elements == null)
			{
				this.m_elements = new List<Element>();
			}
			if (elems.Count > 1)
			{
				IEnumerator<Element> enumerator = elems.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						Element item = enumerator.Current;
						this.m_elements.Add(item);
					}
					return;
				}
			}
			YJKMessageBox.Information("请选择两条以上管线！");
			this.btnRechoose_Click(null, null);
		}

		private void GetSelectionElems(IList<Element> elems)
		{
			if (this.m_elements == null)
			{
				this.m_elements = new List<Element>();
			}
			if (elems.Count > 0)
			{
				IEnumerator<Element> enumerator = elems.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						Element item = enumerator.Current;
						this.m_elements.Add(item);
					}
					return;
				}
			}
			List<Element> list = new List<Element>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_rvtDoc);
			list.AddRange(filteredElementCollector.OfClass(typeof(Pipe)).ToElements());
			this.m_elements.Clear();
			foreach (Element item2 in list)
			{
				this.m_elements.Add(item2);
			}
		}

		private void GetOffValue()
		{
			if (this.userSetting == null)
			{
				this.userSetting = new UserSetting();
			}
			if (this.userSetting.GetValue("PipeChecked", "offset") == "")
			{
				this.m_doffset = 0.0;
			}
			else
			{
				this.m_doffset = double.Parse(this.userSetting.GetValue("PipeChecked", "offset"));
			}
			this.hytbOffset.Text = this.m_doffset.ToString();
		}

		private void SetOffValue()
		{
			if (this.userSetting == null)
			{
				this.userSetting = new UserSetting();
			}
			this.userSetting.WriteValue("PipeChecked", "offset", this.hytbOffset.Text.Trim());
		}

		public void AddPipeinfoToDataGrid(List<Pipe> pipes)
		{
			try
			{
				for (int i = 0; i < pipes.Count; i++)
				{
					Parameter parameter = pipes[i].GetParameter("标记");
					Parameter parameter2 = pipes[i].GetParameter("长度");
					Parameter parameter3 = pipes[i].GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
					object[] values = new object[]
					{
						parameter.AsString(),
						this.m_rvtDoc.GetElement(parameter3.AsElementId()).Name,
						pipes[i].PipeType.Name,
						parameter2.AsValueString(),
						pipes[i].Id
					};
					this.dataGridView1.Rows.Add(values);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
		}

		public void Resolve()
		{
			List<Pipe> list = new List<Pipe>();
			new List<Duct>();
			if (this.m_rvtSel.GetElementIds().Count == 0)
			{
				if (this.m_elements.Count == 0)
				{
					List<Element> list2 = new List<Element>();
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_rvtDoc);
					list2.AddRange(filteredElementCollector.OfClass(typeof(Pipe)).ToElements());
					this.m_elements.Clear();
					using (List<Element>.Enumerator enumerator = list2.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Element element = enumerator.Current;
							this.m_elements.Add(element);
							list.AddRange(this.Resolve(element as Pipe));
						}
						goto IL_14A;
					}
				}
				using (List<Element>.Enumerator enumerator = this.m_elements.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element2 = enumerator.Current;
						if (element2 is Pipe)
						{
							list.AddRange(this.Resolve(element2 as Pipe));
						}
					}
					goto IL_14A;
				}
			}
			foreach (Element element3 in this.m_elements)
			{
				if (element3 is Pipe)
				{
					list.AddRange(this.Resolve(element3 as Pipe));
				}
			}
			IL_14A:
			this.RemoveRepeat(list);
			this.AddPipeinfoToDataGrid(list);
		}

		private List<Pipe> Resolve(Pipe pipe)
		{
			List<Pipe> list = new List<Pipe>();
			Line boundLine = (pipe.Location as LocationCurve).Curve as Line;
			List<ReferenceWithContext> list2 = this.m_detector.Obstructions(boundLine);
			this.Filter(pipe, list2);
			if (list2.Count == 0)
			{
				return list;
			}
			list.Add(pipe);
			foreach (ReferenceWithContext referenceWithContext in list2)
			{
				Reference reference = referenceWithContext.GetReference();
				Element element = this.m_rvtDoc.GetElement(reference);
				if (element.Id != pipe.Id && element is Pipe && !list.Contains(element as Pipe))
				{
					list.Add(element as Pipe);
				}
			}
			this.RemoveRepeat(list);
			return list;
		}

		private void Filter(Pipe pipe, List<ReferenceWithContext> refs)
		{
			for (int i = refs.Count - 1; i >= 0; i--)
			{
				Reference reference = refs[i].GetReference();
				Element element = this.m_rvtDoc.GetElement(reference);
				if (element.Id == pipe.Id || (!(element is Pipe) && !(element is Duct) && element.Category.Id.IntegerValue != -2001320))
				{
					refs.RemoveAt(i);
				}
			}
		}

		private void RemoveRepeat(List<Pipe> pipes)
		{
			List<Pipe> list = new List<Pipe>();
			list.AddRange(pipes);
			for (int i = 0; i < list.Count; i++)
			{
				for (int j = i + 1; j < list.Count; j++)
				{
					if (list.ElementAt(j).Id == list.ElementAt(i).Id && pipes.Contains(list[j]))
					{
						pipes.Remove(list[j]);
					}
				}
			}
		}

		private List<List<Pipe>> CheckPipe()
		{
			List<List<Pipe>> result = new List<List<Pipe>>();
			foreach (List<YJKPiping<Pipe>> hyPipings in this.GroupByPipes())
			{
				this.GetCoveredPipes(hyPipings, ref result);
			}
			return result;
		}

		private void GetCoveredPipes(List<YJKPiping<Pipe>> hyPipings, ref List<List<Pipe>> listPipe)
		{
			for (int i = 0; i < hyPipings.Count; i++)
			{
				List<Pipe> list = new List<Pipe>();
				list.Add(hyPipings[i].Piping);
				for (int j = i + 1; j < hyPipings.Count; j++)
				{
					if (hyPipings[i].IsPipingOnLine(hyPipings[j].Piping))
					{
						if (this.TwoOnLinePipesHasInsection(hyPipings[i], hyPipings[j], false))
						{
							list.Add(hyPipings[j].Piping);
							hyPipings.RemoveAt(j);
							j--;
						}
					}
					else if (this.TwoOnLinePipesHasInsection(hyPipings[i], hyPipings[j], true))
					{
						double distance = hyPipings[i].GetDistance(hyPipings[j].Piping);
						this.m_doffset = hyPipings[i].PipeOutDiameter / 2.0 + hyPipings[j].PipeOutDiameter / 2.0 + this.m_doffset / 304.8;
						double num = hyPipings[i].InsulationThickness;
						num += hyPipings[j].InsulationThickness;
						this.m_doffset += num;
						if (distance < this.m_doffset)
						{
							list.Add(hyPipings[j].Piping);
							hyPipings.RemoveAt(j);
							j--;
						}
					}
				}
				if (list.Count > 1)
				{
					listPipe.Add(list);
				}
			}
		}

		private List<List<YJKPiping<Pipe>>> GroupByPipes()
		{
			List<List<YJKPiping<Pipe>>> list = new List<List<YJKPiping<Pipe>>>();
			List<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
			foreach (Element element in this.m_elements)
			{
				list2.Add(new YJKPiping<Pipe>(element as Pipe));
			}
            double maxDistance = list2.Max((YArchitech.Revit.YJKPiping<Pipe> hyPipe) => hyPipe.InsulationThickness + hyPipe.PipeOutDiameter);
			foreach (List<YJKPiping<Pipe>> pipings in this.GroupByPipeVector(list2))
			{
				List<List<YJKPiping<Pipe>>> list3 = this.GroupBySpecDistance(pipings, maxDistance);
				if (list3.Count > 0)
				{
					list.AddRange(list3);
				}
			}
			return list;
		}

		private List<List<YJKPiping<Pipe>>> GroupByPipeVector(List<YJKPiping<Pipe>> pipings)
		{
			List<List<YJKPiping<Pipe>>> list = new List<List<YJKPiping<Pipe>>>();
			for (int i = 0; i < pipings.Count; i++)
			{
				List<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
				list2.Add(pipings[i]);
				XYZ pipeVector = pipings[i].PipeVector;
				for (int j = i + 1; j < pipings.Count; j++)
				{
					if (pipings[j].PipeVector.IsAlmostEqualTo(pipeVector, 0.0001) || pipings[j].PipeVector.IsAlmostEqualTo(-1.0 * pipeVector, 0.0001))
					{
						list2.Add(pipings[j]);
						pipings.RemoveAt(j);
						j--;
					}
				}
				list.Add(list2);
			}
			return list;
		}

		private List<List<YJKPiping<Pipe>>> GroupBySpecDistance(List<YJKPiping<Pipe>> pipings, double maxDistance)
		{
			List<List<YJKPiping<Pipe>>> list = new List<List<YJKPiping<Pipe>>>();
			if (pipings == null || pipings.Count < 2)
			{
				return list;
			}
			for (int i = 0; i < pipings.Count; i++)
			{
				List<YJKPiping<Pipe>> list2 = new List<YJKPiping<Pipe>>();
				list2.Add(pipings[i]);
				for (int j = i + 1; j < pipings.Count; j++)
				{
					if (Math.Abs(pipings[i].DistanceToSpecProPoint - pipings[j].DistanceToSpecProPoint) < maxDistance)
					{
						list2.Add(pipings[j]);
						pipings.RemoveAt(j);
						j--;
					}
				}
				list.Add(list2);
			}
			return list;
		}

		private bool JustHasBefore(ref List<List<Pipe>> pipeLists, List<Pipe> pipes)
		{
			bool flag = false;
			if (pipeLists.Count == 0)
			{
				return flag;
			}
			foreach (List<Pipe> list in pipeLists)
			{
				foreach (Pipe item in pipes)
				{
					if (list.Contains(item))
					{
						flag = true;
					}
				}
				if (flag)
				{
					using (List<Pipe>.Enumerator enumerator2 = pipes.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Pipe item2 = enumerator2.Current;
							if (!list.Contains(item2))
							{
								list.Add(item2);
							}
						}
						break;
					}
				}
			}
			return flag;
		}

		private bool twoOnLinePipesHasInsection(Pipe pipeA, Pipe pipeB)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipeA);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipeB);
			return hypiping.IsPointOnPipingCurve(hypiping2.StartConnector.Origin) || hypiping.IsPointOnPipingCurve(hypiping2.EndConnector.Origin) || hypiping2.IsPointOnPipingCurve(hypiping.StartConnector.Origin) || hypiping2.IsPointOnPipingCurve(hypiping.EndConnector.Origin);
		}

		private bool TwoOnLinePipesHasInsection(YJKPiping<Pipe> hyPipeA, YJKPiping<Pipe> hyPipeB, bool beParalle = false)
		{
			if (beParalle)
			{
				return hyPipeA.IsPointOnPipingCurve(hyPipeA.GetProject(hyPipeB.StartConnector.Origin, true)) || hyPipeA.IsPointOnPipingCurve(hyPipeA.GetProject(hyPipeB.EndConnector.Origin, true)) || hyPipeB.IsPointOnPipingCurve(hyPipeB.GetProject(hyPipeA.StartConnector.Origin, true)) || hyPipeB.IsPointOnPipingCurve(hyPipeB.GetProject(hyPipeA.EndConnector.Origin, true));
			}
			return hyPipeA.IsPointOnPipingCurve(hyPipeB.StartConnector.Origin) || hyPipeA.IsPointOnPipingCurve(hyPipeB.EndConnector.Origin) || hyPipeB.IsPointOnPipingCurve(hyPipeA.StartConnector.Origin) || hyPipeB.IsPointOnPipingCurve(hyPipeA.EndConnector.Origin);
		}

		private bool TwoParallelLineHasInsectionInProjection(Pipe pipeA, Pipe pipeB)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipeA);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipeB);
			return hypiping.IsPointOnPipingCurve(hypiping.GetProject(hypiping2.StartConnector.Origin, true)) || hypiping.IsPointOnPipingCurve(hypiping.GetProject(hypiping2.EndConnector.Origin, true)) || hypiping2.IsPointOnPipingCurve(hypiping2.GetProject(hypiping.StartConnector.Origin, true)) || hypiping2.IsPointOnPipingCurve(hypiping2.GetProject(hypiping.EndConnector.Origin, true));
		}

		private TreeView ShowTreeview()
		{
			TreeView treeView = new TreeView();
			List<List<Pipe>> list = this.CheckPipe();
			int num = 1;
			foreach (List<Pipe> list2 in list)
			{
				TreeNode treeNode = new TreeNode("重合管线组 " + num.ToString());
				foreach (Pipe pipe in list2)
				{
					string pipeInfo = this.GetPipeInfo(pipe);
					TreeNode treeNode2 = new TreeNode(pipeInfo);
					treeNode2.Name = pipeInfo;
					treeNode2.Tag = pipe.Id;
					treeNode2.ToolTipText = pipeInfo;
					treeNode.Nodes.Add(treeNode2);
				}
				treeView.Nodes.Add(treeNode);
				num++;
			}
			return treeView;
		}

		private string GetPipeInfo(Pipe pipe)
		{
			Parameter parameter = pipe.GetParameter("标记");
			Parameter parameter2 = pipe.GetParameter("长度");
			Parameter parameter3 = pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
			return string.Concat(new string[]
			{
				"管道: 标记 ",
				parameter.AsString(),
				" ",
				this.m_rvtDoc.GetElement(parameter3.AsElementId()).Name,
				" ",
				pipe.PipeType.Name,
				" 长度：",
				parameter2.AsValueString(),
				"mm"
			});
		}

		private void button_OK_Click(object sender, EventArgs e)
		{
			this.SetOffValue();
			using (SubTransaction subTransaction = new SubTransaction(this.m_rvtDoc))
			{
				try
				{
					if ((int)subTransaction.Start() == 1 && (int)subTransaction.Commit() == 3)
					{
						this.UpdateModel(true);
					}
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					MessageBox.Show(ex.Message, "盈建科提示");
				}
			}
			base.Close();
			base.DialogResult = DialogResult.OK;
		}

		private void button_delete_Click(object sender, EventArgs e)
		{
			using (SubTransaction subTransaction = new SubTransaction(this.m_rvtDoc))
			{
				try
				{
					if ((int)subTransaction.Start() == 1)
					{
						TreeNode selectedNode = this.m_tv.SelectedNode;
						if (selectedNode.Nodes.Count > 0)
						{
							if (YJKMessageBox.Show("是否删除整个管线组？", MessageBoxButtons.YesNo) == DialogResult.No && (int)subTransaction.Commit() == 3)
							{
								return;
							}
							foreach (object obj in selectedNode.Nodes)
							{
								TreeNode treeNode = (TreeNode)obj;
								this.m_rvtDoc.Delete(treeNode.Tag as ElementId);
							}
							this.m_tv.Nodes.Remove(selectedNode);
						}
						else
						{
							this.m_rvtDoc.Delete(selectedNode.Tag as ElementId);
							this.m_SelElem = null;
							if (selectedNode.Parent.Nodes.Count > 2)
							{
								this.m_tv.Nodes.Remove(selectedNode);
							}
							else
							{
								this.m_tv.Nodes.Remove(selectedNode.Parent);
							}
						}
						this.m_rvtDoc.Regenerate();
						if ((int)subTransaction.Commit() == 3)
						{
							this.UpdateModel(true);
						}
					}
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					YJKMessageBox.Warning(ex.Message);
				}
			}
		}

		private void button_move_Click(object sender, EventArgs e)
		{
		}

		private void button_copy_Click(object sender, EventArgs e)
		{
		}

		private void dataGridView1_Click(object sender, EventArgs e)
		{
		}

		private void UpdateModel(bool autoJoin)
		{
			this.m_rvtDoc.Regenerate();
			if (autoJoin)
			{
				this.m_rvtDoc.AutoJoinElements();
			}
			this.m_uiApp.ActiveUIDocument.RefreshActiveView();
		}

		private void dataGridView1_SelectionChanged(object sender, EventArgs e)
		{
		}

		private void btnRechoose_Click(object sender, EventArgs e)
		{
			this.SetOffValue();
			base.Close();
			base.DialogResult = DialogResult.Retry;
		}

		private void ResultTv_AfterSelect(object sender, TreeViewEventArgs e)
		{
			RevitVersionFuncs.ClearSelection(this.m_rvtSel);
			using (SubTransaction subTransaction = new SubTransaction(this.m_rvtDoc))
			{
				try
				{
					if ((int)subTransaction.Start() == 1)
					{
						this.m_tv.SelectedNode.BackColor = System.Drawing.Color.White;
						this.m_tv.SelectedNode.ForeColor = System.Drawing.Color.Black;
						TreeNode selectedNode = this.m_tv.SelectedNode;
						if (selectedNode.Nodes.Count > 0)
						{
							IEnumerator enumerator = selectedNode.Nodes.GetEnumerator();
							{
								while (enumerator.MoveNext())
								{
									object obj = enumerator.Current;
									TreeNode treeNode = (TreeNode)obj;
									if (!this.IsElementDelet(treeNode.Tag as ElementId))
									{
										this.m_SelElem = this.m_rvtDoc.GetElement(treeNode.Tag as ElementId);
										List<Element> list = new List<Element>();
										list.Add(this.m_SelElem);
										RevitVersionFuncs.SetSelection(this.m_rvtSel, list);
									}
								}
								goto IL_199;
							}
						}
						if (this.IsElementDelet(selectedNode.Tag as ElementId))
						{
							if (selectedNode.Parent.Nodes.Count > 2)
							{
								YJKMessageBox.Information("该管段已被删除。");
								this.m_tv.Nodes.Remove(selectedNode);
							}
							else
							{
								YJKMessageBox.Information("该管段已经删除，此重合已处理。");
								this.m_tv.Nodes.Remove(selectedNode.Parent);
							}
						}
						else
						{
							this.m_SelElem = this.m_rvtDoc.GetElement(selectedNode.Tag as ElementId);
							List<Element> list2 = new List<Element>();
							list2.Add(this.m_SelElem);
							RevitVersionFuncs.SetSelection(this.m_rvtSel, list2);
						}
						IL_199:
						this.m_uiDoc.ShowElements(this.m_rvtSel.GetElementIds());
						if ((int)subTransaction.Commit() == 3)
						{
							this.button_delete.Enabled = true;
							this.UpdateModel(true);
							return;
						}
					}
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					YJKMessageBox.Warning(ex.Message);
				}
			}
			this.ResultTv.Focus();
		}

		private void ResultTv_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.m_tv.SelectedNode != null)
			{
				this.m_tv.SelectedNode.BackColor = System.Drawing.Color.White;
				this.m_tv.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private List<XYZ> GetBoundingBox(TreeNode tn)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = new XYZ(0.0, 0.0, 0.0);
			XYZ xyz2 = new XYZ(0.0, 0.0, 0.0);
			if (tn.Nodes.Count > 0)
			{
				IEnumerator enumerator = tn.Nodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						TreeNode treeNode = (TreeNode)obj;
						if (!this.IsElementDelet(treeNode.Tag as ElementId))
						{
							BoundingBoxXYZ boundingBoxXYZ = this.m_rvtDoc.GetElementById(treeNode.Tag as ElementId).get_BoundingBox(this.m_rvtDoc.ActiveView);
							xyz = this.GetMinPoint(xyz, boundingBoxXYZ.Min);
							xyz2 = this.GetMaxPoint(xyz2, boundingBoxXYZ.Max);
						}
					}
					goto IL_120;
				}
			}
			BoundingBoxXYZ boundingBoxXYZ2 = this.m_rvtDoc.GetElementById(tn.Tag as ElementId).get_BoundingBox(this.m_rvtDoc.ActiveView);
			xyz = boundingBoxXYZ2.Min;
			xyz2 = boundingBoxXYZ2.Max;
			IL_120:
			list.Insert(0, xyz);
			list.Insert(1, xyz2);
			return list;
		}

		private XYZ GetMinPoint(XYZ p1, XYZ p2)
		{
			double num = (p1.X < p2.X) ? p1.X : p2.X;
			double num2 = (p1.Y < p2.Y) ? p1.Y : p2.Y;
			double num3 = (p1.Z < p2.Z) ? p1.Z : p2.Z;
			return new XYZ(num, num2, num3);
		}

		private XYZ GetMaxPoint(XYZ p1, XYZ p2)
		{
			double num = (p1.X > p2.X) ? p1.X : p2.X;
			double num2 = (p1.Y > p2.Y) ? p1.Y : p2.Y;
			double num3 = (p1.Z > p2.Z) ? p1.Z : p2.Z;
			return new XYZ(num, num2, num3);
		}

		private bool IsElementDelet(ElementId elemId)
		{
			bool result = false;
			List<ElementId> list = new List<ElementId>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_rvtDoc);
			list.AddRange(filteredElementCollector.OfClass(typeof(Pipe)).ToElementIds());
			if (!list.Contains(elemId))
			{
				result = true;
			}
			return result;
		}

		private void UpdateTreeView()
		{
			foreach (object obj in this.m_tv.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				foreach (object obj2 in treeNode.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj2;
					if (this.IsElementDelet(treeNode2.Tag as ElementId))
					{
						treeNode.Nodes.Remove(treeNode2);
					}
				}
				if (treeNode.Nodes.Count < 2)
				{
					this.m_tv.Nodes.Remove(treeNode);
				}
			}
		}

		private void hytbOffset_Validated(object sender, EventArgs e)
		{
			if (double.Parse(this.hytbOffset.Text.Trim()) >= 1000.0)
			{
				YJKMessageBox.Warning("管道外壁间距不得大于1000mm，请重设！");
				this.hytbOffset.Undo();
			}
		}

		private Document m_rvtDoc;

        private Autodesk.Revit.ApplicationServices.Application m_rvtApp;

		private Selection m_rvtSel;

		private Detector m_detector;

		private Element m_SelElem;

		private UIApplication m_uiApp;

		private UIDocument m_uiDoc;

		public Transaction m_transaction;

		private List<Element> m_elements;

		private double m_doffset;

		private TreeView m_tv;

		private UserSetting userSetting;
	}
}
